﻿using sc;
using System.ComponentModel;
using System.Windows.Input;

namespace MauiApp1.ViewModels
{
    public class MainPageViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void NotifyPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public string Title
        {
            get { return _title; }
            set
            {
                _title = value;
                NotifyPropertyChanged("Title");
            }
        }
        private string _title;

        public ICommand ClickMe
        {
            get
            {
                return new Command(() =>
                {
                    Title += "Q";
                });
            }
        }

        public ICommand Login
        {
            get
            {
                return new Command(async () =>
                {
                    using (MainServiceSoapClient mainServiceSoapClient = new sc.MainServiceSoapClient(MainServiceSoapClient.EndpointConfiguration.MainServiceSoap/*, "https://localhost:44352/MainService.asmx"*/))
                    {
                        try
                        {
                            mainServiceSoapClient.Open();
                            CommonInterfaceRequest request = new CommonInterfaceRequest();
                            request.Body = new CommonInterfaceRequestBody() { requeststring = "aaaaaaaaaaaa" };
                            CommonInterfaceResponse res = await mainServiceSoapClient.CommonInterfaceAsync(request);
                            Title = res.Body.CommonInterfaceResult.ToString();
                        }
                        catch (Exception ex)
                        {

                        }
                        finally
                        {
                            mainServiceSoapClient.Close();
                        }
                    }
                });
            }
        }
    }


    ////ViewModel需继承
    //public class MainViewModel : ObservableObject
    //{
    //    private string _test;
    //    private ObservableCollection<MainModel> _temps;
    //    private ICommand addCommand;
    //    private ICommand deleteCommand;

    //    //数据通知集合
    //    public ObservableCollection<MainModel> Temps { get => _temps; set => _temps = value; }

    //    //命令
    //    public ICommand AddCommand { get => addCommand ?? (addCommand = new RelayCommand(AddCallback)); }
    //    public ICommand DeleteCommand { get => deleteCommand ?? (deleteCommand = new RelayCommand(DeleteCallback)); }

    //    //数据通知字段
    //    public string Test { get => _test; set => SetProperty(ref _test, value); }

    //    public MainViewModel()
    //    {
    //        //初始化
    //        Temps = new ObservableCollection<MainModel>();
    //        Temps.Add(new MainModel { Name = "zhangsan" });
    //        Temps.Add(new MainModel { Name = "zhangsan" });
    //    }

    //    //命令执行内容
    //    private void AddCallback()
    //    {
    //        Temps.Add(new MainModel { Name = DateTime.Now.ToString() });
    //    }

    //    private void DeleteCallback()
    //    {
    //        Temps.RemoveAt(0);
    //    }
    //}
}
